<?php

namespace app\controller\api;

use app\model\api\Mini as MiniModel;
use app\model\api\WorldCategory;
use app\model\api\WorldGood;
use laytp\controller\Api;
use think\exception\ValidateException;

/**
 * Class World
 * @package app\index\controller
 */
class World extends Api
{
    protected $model;

    protected function initialize()
    {
        $this->model = new WorldGood();
    }


    /**
     * 我的世界图鉴
     * @return false|string|\think\response\Json
     */
    public function list()
    {
        $data = input("post.");
        $keyword = !empty($data['keyword']) ? $data['keyword']:"";
        $category = !empty($data['category']) ? $data["category"]:"";
        $sub_category = !empty($data['sub_category']) ? $data["sub_category"]:"";
        $page = !empty($data['page']) ? $data['page'] : 1;
        try {
            //验证器
            validate(\app\validate\api\World::class)
                ->scene('list')
                ->check([
                    'page' => $page
                ]);

            $list = $this->model->list($category,$sub_category,$keyword);
            return $this->success('success', $list['data'], array(
                'total' => $list['total'],
                'current_page' => $list['current_page'],
                'last_page' => $list['last_page'],
                'per_page' => $list['per_page']
            ));

        } catch (ValidateException $exception) {
            return $this->error($exception->getMessage(), -1);
        }
    }

    /**
     * 详情
     * @return false|string|\think\response\Json
     */
    public function detail()
    {
        $data = input('post.');
        $good_id = !empty($data['good_id']) ? $data['good_id']:'';
        try {
            //验证
            validate(\app\validate\api\World::class)
                ->scene('detail')
                ->check([
                    'good_id' => $good_id
                ]);
            $detail = $this->model->detail($good_id);
            return $this->success('success', $detail);
        } catch (ValidateException $exception) {
            return $this->error($exception->getMessage(), -1);
        }
    }


    /**
     * 我的世界分类
     * @return false|string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function category()
    {
        try {
            $list = (new WorldCategory())->list();
            return $this->success('success', $list);
        } catch (ValidateException $exception) {
            return $this->error($exception->getMessage(), -1);
        }
    }
}